AppConnect SDK limitation with SwiftUI

The SwiftUI app is used to build app screens using views like text, buttons, and images. You can use the SwiftUI app to perform the following functions:

  • Control layout

  • Handle user actions such as taps

  • Support code sharing across different Apple devices.

App lifecycle structure

AppConnect SDK Structure using UIKit

  • main.swift: This file serves as the entry point for a UIKit-based iOS application. It contains a call to UIApplicationMain, which performs the following key tasks:

    • Initializes the app’s runtime environment.

    • Creates the shared UIApplication singleton instance, which manages app-level behavior and interactions.

    • Creates the AppDelegate object which manages tasks such as responding to app launch, managing transitions to the background and preparing the app to close.

    • The following line starts the application:

      This call directs the system to use AppDelegate to handle key app behaviors.

  • UIApplication: It is the central part of an iOS application that manages its behavior and makes sure everything works together. For example, responding to user actions, updating the screen, and communicating with the system.

  • AppDelegate: (Conforms to UIApplicationDelegate): The AppDelegate class acts as the central coordinator for your app. It contains methods that respond to key changes in the application state, For example, when the app launches, moves to the background, or shuts down.

SwiftUI

SwiftUI introduces a more declarative and modular way to manage the app lifecycle. It uses the App and Scene protocols to define how the app starts and displays its content. It replaces the AppDelegate and UIApplicationMain setup used in UIKit.

App Protocol

  • Defines the main entry point of the app.

  • Declares one or more Scenes (UI components, such as windows or tabs).

  • Manages app-level state and configuration.

Scene Protocol

  • Declares UI containers such as windows, tabs, or documents.

  • Manages view presentation and content structure.

  • Handles scene-specific-state, such as focus or activity.

  • Supports multiple windows and scenes. For example, WindowGroup represents the main content window for iOS apps.

UIKit-SwiftUI compatibility issues

The AppConnect SDK functions on the UIKit structure, while SwiftUI handles the app life cycle using App and Scene protocols. It differs from AppConnect SDKs UIKit’s AppDelegate and UIApplicationDelegate protocols.

This difference between SwiftUI and UIKit causes system failures as AppConnect SDK currently supports AppConnectUIApplication and SwiftUI app is of type SwiftUIApplication. This prevents AppConnect SDK integration with SwiftUI apps.

Enabling AppConnect SDK with SwiftUI

The following changes describes alternative methods to enable AppConnect SDK support in a SwiftUI app.

Changes for UIApplicationDelegate protocol:

  1. Create an AppDelegate class for Swift app.

  2. Use the @UIApplicationDelegateAdaptor property wrapper with your AppDelegate class.

Handling Send Event protocol:

Since Send Event is now managed within the AppConnect SDK, no change is needed for SwiftUI integration.

Full support with AppConnect SDK is not possible for wrapped SwiftUI application.

Integrating AppConnect SDK with SwiftUI

The following steps displays how to integrate AppConnect SDK into SwiftUI application.

Before you begin 

  1. Xcode Install: Install the latest version of Xcode on your machine.

  2. SwiftUI Project: Create a SwiftUI based iOS app project in Xcode.

  3. AppConnect SDK: Add the AppConnect SDK to your project.

  4. Setup Requirements: Know your Xcode project structure and basic knowledge of SwiftUI.

Procedure 

  1. In main.swift, add the following code:

  2. Insert code below in appDelegate.swift for app sdk initialization:

    3. Build your app in Xcode.